package com.xjmall.common;

import org.codehaus.jackson.map.annotate.JsonSerialize;

import java.io.Serializable;

/**
 * 服务器统一返回模板
 * Created by An on 2017/8/21.
 */
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
public class ServerResult<T> implements Serializable {

    private int code; // 返回代码
    private String message; // 返回信息
    private T data; // 返回数据

    public ServerResult(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public ServerResult(int code, T data) {
        this.code = code;
        this.data = data;
    }

    public ServerResult(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    /**
     * 返回成功信息
     * @param message 待返回的信息
     * @return ServerResult
     */
    public static ServerResult successMsg(String message) {
        return new ServerResult(ResponseCode.SUCCESS.getCode(), message);
    }

    /**
     * 返回成功数据
     * @param data 待返回的数据
     * @param <T>
     * @return ServerResult
     */
    public static <T> ServerResult<T> successData(T data) {
        return new ServerResult<T>(ResponseCode.SUCCESS.getCode(), data);
    }

    /**
     * 返回成功信息和数据
     * @param message 待返回的信息
     * @param data 待返回的数据
     * @param <T>
     * @return ServerResult
     */
    public static <T> ServerResult<T> successMsgAndData(String message, T data) {
        return new ServerResult<T>(ResponseCode.SUCCESS.getCode(), message, data);
    }


    public int getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }

    public T getData() {
        return data;
    }

}
